var pageii = null;
var grid = null;
$(function () {
    grid = lyGrid({
        id: 'paging',
        l_column: [{
            colkey: "id",
            name: "id",
            hide: true
        }, {
            colkey: "userName",
            name: "姓名"
        }, {
            colkey: "phone",
            name: "注册手机"
        }, {
            colkey: "userAddr",
            name: "车牌"
        },  {
            colkey: "idnum",
            name: "身份证"
        }, {
            colkey: "jlz",
            name: "教练证编号"
        }, {
            colkey: "isAuth",
            name: "认证状态",
            renderData: function (rowindex, data, rowdata, column) {
                console.log(data)
                if (data == "Y") {
                    return "<label class='label label-success'>已经认证</label>";
                }
                if (data == "N") {
                    return "<label class='label label-danger'>未上传教练证</label>";
                }
                if (data == "Z") {
                    return "<label class='label label-primary'>等待认证</label>";
                }
            }
        }, {
            colkey: "createTime",
            name: "注册时间",
            width: '160px',
            renderData: function (rowindex, data, rowdata, column) {
                return new Date(data).format("yyyy-MM-dd hh:mm:ss");
            }
        }],
        jsonUrl: rootPath + '/resident/findByPage2.shtml',
        checkbox: true,
        serNumber: true
    });
    $("#search").click("click", function () {// 绑定查询按扭
        var searchParams = $("#searchForm").serializeJson();// 初始化传参数
        grid.setOptions({
            data: searchParams
        });
    });
    $("#addResident").click("click", function () {
        addResident();
    });
    $("#editResident").click("click", function () {
        editResident();
    });
    $("#authResident").click("click", function () {
        authResident();
    });
    $("#info").click("click", function () {
        info();
    });
    $("#course").click("click", function () {
        course();
    });
    $("#handsel").click("click", function () {
        handsel();
    });
    $("#integralList").click("click", function () {
        integralList();
    });
});
function checkstate(obj, id) {
    var d = 'N';
    if (obj.checked) {
        d = 'Y';
    }
    var url = rootPath + '/resident/updateState.shtml';
    var s = CommnUtil.ajax(url, {
        "residentFormMap.id": id,
        "residentFormMap.isAuth": d
    }, "json");
    if (s == "success") {
        layer.msg('操作成功');
    } else {
        layer.msg('操作失败');
    }
}
function checkMaster(obj, id) {
    var d = 'N';
    if (obj.checked) {
        d = 'Y';
    }
    var url = rootPath + '/resident/updateMaster.shtml';
    var s = CommnUtil.ajax(url, {
        "residentFormMap.id": id,
        "residentFormMap.isMaster": d
    }, "json");
    if (s == "success") {
        layer.msg('操作成功');
    } else {
        layer.msg('操作失败');
    }
}
function editResident() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }
    var url = rootPath + '/resident/editUI.shtml?id=' + cbox;
    pageii = layer.open({
        title: "编辑",
        type: 1,
        area: ["600px", "80%"],
        content: CommnUtil.ajax(url)
    });
}

function info() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("请选中需要查看的教练，并且只能选择一个");
        return;
    }
    var url = rootPath + '/resident/info.shtml?id=' + cbox;
    pageii = layer.open({
        title: "教练证信息",
        type: 1,
        area: ["600px", "80%"],
        content: CommnUtil.ajax(url)
    });
}
function course() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("请选中需要查看的教练，并且只能选择一个");
        return;
    }
    var url = rootPath + '/resident/course.shtml?id=' + cbox;
    pageii = layer.open({
        title: "排课信息",
        type: 1,
        area: ["600px", "80%"],
        content: CommnUtil.ajax(url)
    });
}
function addResident() {
    var url = rootPath + '/resident/addUI.shtml';
    pageii = layer.open({
        title: "新增",
        type: 1,
        area: ["600px", "auto"],
        content: CommnUtil.ajax(url)
    });
}


function authResident() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {
        layer.msg("请选择需要认证的教练");
        return;
    }
    
    layer.confirm('是否认证通过？',
    		{btn:['是','否']}, function () {
    			
        var url = rootPath + '/resident/auth.shtml';
        var s = CommnUtil.ajax(url, {
            ids: cbox.join(",")
        }, "json");
        if (s == "success") {
            layer.msg('认证成功');
            grid.loadData();
        } else {
            layer.msg('认证失败');
        }
    },function(){
    	 var url = rootPath + '/resident/notauth.shtml';
         var s = CommnUtil.ajax(url, {
             ids: cbox.join(",")
         }, "json");
         if (s == "success") {
             layer.msg('取消认证成功');
             grid.loadData();
         } else {
             layer.msg('取消认证失败');
         }
    	
    });
}